//
//  PersonalIdentificationViewController.m
//  ChatForHot
//
//  Created by wr on 2018/10/10.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalIdentificationViewController.h"
#import "HCHelper.h"

@interface PersonalIdentificationViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic,strong) UILabel *HOTCHATinfoLabel;
@property (nonatomic,strong) UIImageView *HOTCHATpickImageView;
@property (nonatomic,strong) UIImageView *HOTCHATIdnAvatarImageView;
@property (nonatomic,strong) UIImageView *HOTCHATpickMarkView;
@property (nonatomic,strong) UIButton *HOTCHATsaveBtn;
@end

@implementation PersonalIdentificationViewController

- (void)HOTCHATsetNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated {
    [super HOTCHATsetNavigationItemsIsInEditMode:isInEditMode animated:animated];
    
    self.title = NSLocalizedString(@"person9", nil);
}

- (void)HOTCHATInsertSubViews {
    [super HOTCHATInsertSubViews];
    
    self.HOTCHATinfoLabel = [[UILabel alloc] init];
    self.HOTCHATinfoLabel.font = [UIFont fontWithName:@"ArialMT" size:15];
    self.HOTCHATinfoLabel.textColor = [UIColor colorFromHexString:@"AAAAAA"];
    self.HOTCHATinfoLabel.text = NSLocalizedString(@"Identification1", nil);
    self.HOTCHATinfoLabel.textAlignment = NSTextAlignmentCenter;
    self.HOTCHATinfoLabel.numberOfLines = 0;
    [self.HOTCHATinfoLabel changeLineSpaceForLabelWithSpace:10];
    [self.view addSubview:self.HOTCHATinfoLabel];
    
    self.HOTCHATpickImageView = [[UIImageView alloc] init];
    self.HOTCHATpickImageView.layer.masksToBounds = YES;
    self.HOTCHATpickImageView.layer.cornerRadius = 210 / 2;
    [self.view addSubview:self.HOTCHATpickImageView];
    
    self.HOTCHATIdnAvatarImageView = [[UIImageView alloc] init];
    self.HOTCHATIdnAvatarImageView.layer.masksToBounds = YES;
    self.HOTCHATIdnAvatarImageView.layer.cornerRadius = 210 / 2;
    [self.view addSubview:self.HOTCHATIdnAvatarImageView];
    
    @weakify(self)
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self)
        [self HOTCHATOpenTheCamera];
    }];
    self.HOTCHATpickImageView.userInteractionEnabled = YES;
    [self.HOTCHATpickImageView addGestureRecognizer:tap];
    
    self.HOTCHATpickMarkView = [[UIImageView alloc] init];
    [self.view addSubview:self.HOTCHATpickMarkView];
    
    self.HOTCHATsaveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.HOTCHATsaveBtn setBackgroundImage:[[UIImage imageNamed:@"personal_iden_sav_n"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 15) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateNormal];
    [self.HOTCHATsaveBtn setBackgroundImage:[[UIImage imageNamed:@"personal_iden_sav_h"]resizableImageWithCapInsets:UIEdgeInsetsMake(15, 15, 15, 15) resizingMode:(UIImageResizingModeStretch)] forState:UIControlStateHighlighted];
    [self.HOTCHATsaveBtn setTitle:NSLocalizedString(@"identification3", nil) forState:UIControlStateNormal];
    [self.HOTCHATsaveBtn setTitle:NSLocalizedString(@"identification3", nil) forState:UIControlStateHighlighted];
    [self.view addSubview:self.HOTCHATsaveBtn];
    [[self.HOTCHATsaveBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self)
        if ([[APPINFO defaultManager].isApprove isEqualToString:@"1"]) {
             [self.navigationController popViewControllerAnimated:YES];
        }else {
            if (self.HOTCHATIdnAvatarImageView.image && [[APPINFO defaultManager].isApprove isEqualToString:@"0"]) {
                [self HOTCHATUploadIdenPhoto:self.HOTCHATIdnAvatarImageView.image];
            }
        }
    }];
}
#pragma mark - addPhotoActions

- (void)HOTCHATOpenTheCamera {
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    }else{
        return;
    }
    [imagePicker setDelegate:self];
    [self presentViewController:imagePicker animated:YES completion:nil];
}

- (void)HOTCHATUploadIdenPhoto:(UIImage *)image {
    [self HOTCHATstartLoading];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:[APPINFO defaultManager].accountid forKey:@"accountid"];
    [NetWorkTool HOTCHATUploadImageData:UPLOADIden_URL parameters:parameters fileData:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:image.HOTCHATcompressQualityData name:@"uploadfile" fileName:@"headpic.jpg" mimeType:@"image/jpeg"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull HOTCHATtask, NSString * _Nonnull HOTCHATmsgCode, NSDictionary * _Nonnull HOTCHATresultDic) {
        [self HOTCHATstopLoading];
        //刷新token
        [NetWorkTool HOTCHATUpdateToken];
    } failure:^(NSURLSessionDataTask * _Nonnull HOTCHATtask, NSError * _Nonnull error) {
        [self HOTCHATstopLoading];
    }];
}

#pragma mark - imagePickerDelegate

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    portraitImg = [[HCHelper sharedInstance] HOTCHATimageCompressToMaxSize:portraitImg];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    self.HOTCHATIdnAvatarImageView.image = portraitImg;
}

- (void)HOTCHATidenSuccessLayout {
    self.HOTCHATinfoLabel.text = NSLocalizedString(@"identification2", nil);
    [self.HOTCHATpickMarkView setImage:[UIImage imageNamed:@"personal_iden_mark_s"]];
    self.HOTCHATpickImageView.userInteractionEnabled = NO;
    [self.HOTCHATIdnAvatarImageView simple_setImageWithURL:[[APPINFO defaultManager].approvePic HOTCHATTotalStringUrl]placeholderImage:[UIImage imageNamed:@"photo_placeholder"]];
    [self.HOTCHATsaveBtn setTitle:NSLocalizedString(@"identification4", nil) forState:UIControlStateNormal];
    [self.HOTCHATsaveBtn setTitle:NSLocalizedString(@"identification4", nil) forState:UIControlStateHighlighted];
}

- (void)HOTCHATidenNormalLayout {
    self.HOTCHATinfoLabel.text = NSLocalizedString(@"identification1", nil);
    [self.HOTCHATpickMarkView setImage:[UIImage imageNamed:@"personal_iden_mark_n"]];
    [self.HOTCHATpickImageView setImage:[UIImage imageNamed:@"personal_atatar_but_n"]];
    self.HOTCHATpickImageView.userInteractionEnabled = YES;
    [self.HOTCHATsaveBtn setTitle:NSLocalizedString(@"identification3", nil) forState:UIControlStateNormal];
    [self.HOTCHATsaveBtn setTitle:NSLocalizedString(@"identification3", nil) forState:UIControlStateHighlighted];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.HOTCHATinfoLabel.size = CGSizeMake(300, 90);
    self.HOTCHATinfoLabel.top = 30;
    self.HOTCHATinfoLabel.centerX = self.view.centerX;
    
    self.HOTCHATpickImageView.size = CGSizeMake(210, 210);
    self.HOTCHATpickImageView.top = self.HOTCHATinfoLabel.bottom + 23;
    self.HOTCHATpickImageView.centerX = self.view.centerX;
    
    self.HOTCHATIdnAvatarImageView.size = CGSizeMake(210, 210);
    self.HOTCHATIdnAvatarImageView.centerX = self.HOTCHATpickImageView.centerX;
    self.HOTCHATIdnAvatarImageView.centerY = self.HOTCHATpickImageView.centerY;
    
    self.HOTCHATpickMarkView.size = CGSizeMake(230, 60);
    self.HOTCHATpickMarkView.top = self.HOTCHATpickImageView.bottom + 32;
    self.HOTCHATpickMarkView.centerX = self.view.centerX;
    
    self.HOTCHATsaveBtn.size = CGSizeMake(265, 44);
    self.HOTCHATsaveBtn.top = self.HOTCHATpickMarkView.bottom + 46;
    self.HOTCHATsaveBtn.centerX = self.view.centerX;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self HOTCHATidenNormalLayout];
    @weakify(self)
    [RACObserve([APPINFO defaultManager], isApprove) subscribeNext:^(id x) {
        @strongify(self)
        if ([[APPINFO defaultManager].isApprove isEqualToString:@"1"]) {
            [self HOTCHATidenSuccessLayout];
        }else{
            [self HOTCHATidenNormalLayout];
        }
    }];
}

@end
